﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;
using System.Text;
//Please add references
namespace ZX.HIS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:DrugUseInfo
    /// </summary>
    public partial class DrugUseInfo : IDrugUseInfo
    {
        public DrugUseInfo()
        { }
        #region Depart自写方法
        /// <summary>
        /// 药品使用信息表添加
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public int DrugUseInfoAdd(ZX.HIS.Model.DrugUseInfo mode)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("");
            sql.AppendLine("INSERT INTO [ZX_HISManager].[dbo].[tbDrugUseInfo]");
            sql.AppendLine("(HospitalId,DrugUsageName,DrugUseIdMeasu,MeasuringUnitName,DrugFrequencyName,DrugUseOpenDose,OpenDoseName,DocAdviceId)");
            sql.AppendLine("VALUES");
            sql.AppendLine("(@HospitalId,@DrugUsageName,@DrugUseIdMeasu,@MeasuringUnitName,@DrugFrequencyName,@DrugUseOpenDose,@OpenDoseName,@DocAdviceId)");
            SqlParameter[] Parameter ={
                                          new SqlParameter("@HospitalId",SqlDbType.Int),
                                          new SqlParameter("@DrugUsageName",SqlDbType.VarChar),
                                          new SqlParameter("@DrugUseIdMeasu",SqlDbType.VarChar),
                                          new SqlParameter("@MeasuringUnitName",SqlDbType.VarChar),
                                          new SqlParameter("@DrugFrequencyName",SqlDbType.VarChar),
                                          new SqlParameter("@DrugUseOpenDose",SqlDbType.VarChar),
                                          new SqlParameter("@OpenDoseName",SqlDbType.VarChar),
                                          new SqlParameter("@DocAdviceId",SqlDbType.VarChar)
                                      };
            Parameter[0].Value = mode.HospitalId;
            Parameter[1].Value = mode.DrugUsageName;
            Parameter[2].Value = mode.DrugUseIdMeasu;
            Parameter[3].Value = mode.MeasuringUnitName;
            Parameter[4].Value = mode.DrugFrequencyName;
            Parameter[5].Value = mode.DrugUseOpenDose;
            Parameter[6].Value = mode.OpenDoseName;
            Parameter[7].Value = mode.DocAdviceId;
            return SqlDBhelper.ExecuteNonQuery(sql.ToString(), Parameter);
        }

        /// <summary>
        /// 药品使用信息表修改返填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ZX.HIS.Model.DrugUseInfo DrugUseInfoUpdShow(int id)
        {
            string ComTex = "Proc_DrugUseInfoModel";
            SqlParameter[] Parameter ={
                                          new SqlParameter("@DrugUseId",SqlDbType.Int){Value=id}
                                      };

            DataSet ds = SqlDBhelper.RunProcedure(ComTex, Parameter,"db");
            if (ds.Tables[0].Rows.Count > 0) 
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZX.HIS.Model.DrugUseInfo DataRowToModel(DataRow row)
        {
            ZX.HIS.Model.DrugUseInfo model = new ZX.HIS.Model.DrugUseInfo();
            if (row != null)
            {
                if (row["DrugUseId"] != null && row["DrugUseId"].ToString() != "")
                {
                    model.DrugUseId = int.Parse(row["DrugUseId"].ToString());
                }
                if (row["HospitalId"] != null && row["HospitalId"].ToString() != "")
                {
                    model.HospitalId = int.Parse(row["HospitalId"].ToString());
                }
                if (row["DrugUsageName"] != null)
                {
                    model.DrugUsageName = row["DrugUsageName"].ToString();
                }
                if (row["DrugUseIdMeasu"] != null && row["DrugUseIdMeasu"].ToString() != "")
                {
                    model.DrugUseIdMeasu = row["DrugUseIdMeasu"].ToString();
                }
                if (row["MeasuringUnitName"] != null)
                {
                    model.MeasuringUnitName = row["MeasuringUnitName"].ToString();
                }
                if (row["DrugFrequencyName"] != null)
                {
                    model.DrugFrequencyName = row["DrugFrequencyName"].ToString();
                }
                if (row["DrugUseOpenDose"] != null && row["DrugUseOpenDose"].ToString() != "")
                {
                    model.DrugUseOpenDose = row["DrugUseOpenDose"].ToString();
                }
                if (row["OpenDoseName"] != null)
                {
                    model.OpenDoseName = row["OpenDoseName"].ToString();
                }
                if (row["DocAdviceId"] != null && row["DocAdviceId"].ToString() != "")
                {
                    model.DocAdviceId = row["DocAdviceId"].ToString();
                }
            }
            return model;
        }
        /// <summary>
        /// 药品使用信息表修改
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public int DrugUseInfoUpd(ZX.HIS.Model.DrugUseInfo mode)
        {
            string ComTex = "Proc_DrugUseInfoUpt";
            SqlParameter[] Parameter ={
                                          new SqlParameter("@DrugUseId",SqlDbType.Int){Value=mode.DrugUseId},
                                          new SqlParameter("@HospitalId",SqlDbType.Int){Value=mode.HospitalId},
                                          new SqlParameter("@DrugUsageName",SqlDbType.VarChar){Value=mode.DrugUsageName},
                                          new SqlParameter("@DrugUseIdMeasu",SqlDbType.VarChar){Value=mode.DrugUseIdMeasu},
                                          new SqlParameter("@MeasuringUnitName",SqlDbType.VarChar){Value=mode.MeasuringUnitName},
                                          new SqlParameter("@DrugFrequencyName",SqlDbType.VarChar){Value=mode.DrugFrequencyName},
                                          new SqlParameter("@DrugUseOpenDose",SqlDbType.VarChar){Value=mode.DrugUseOpenDose},
                                          new SqlParameter("@OpenDoseName",SqlDbType.VarChar){Value=mode.OpenDoseName},
                                          new SqlParameter("@DocAdviceId",SqlDbType.VarChar){Value=mode.DocAdviceId}
                                         
                                      };
            object obj = SqlDBhelper.RunProcedureNonQuery(ComTex, Parameter);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return 0;
        }

        #endregion
        #region  BasicMethod

        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //return DbHelperSQL.GetMaxID("DrugUseId", "tbDrugUseInfo"); 
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(int DrugUseId)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) from tbDrugUseInfo");
        //    strSql.Append(" where DrugUseId=@DrugUseId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugUseId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugUseId;

        //    return DbHelperSQL.Exists(strSql.ToString(),parameters);
        //}


        ///// <summary>
        ///// 增加一条数据
        ///// </summary>
        //public int Add(ZX.HIS.Model.DrugUseInfo model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("insert into tbDrugUseInfo(");
        //    strSql.Append("HospitalId,DrugUsageName,DrugUseIdMeasu,MeasuringUnitName,DrugFrequencyName,DrugUseOpenDose,OpenDoseName,DocAdviceId)");
        //    strSql.Append(" values (");
        //    strSql.Append("@HospitalId,@DrugUsageName,@DrugUseIdMeasu,@MeasuringUnitName,@DrugFrequencyName,@DrugUseOpenDose,@OpenDoseName,@DocAdviceId)");
        //    strSql.Append(";select @@IDENTITY");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DrugUsageName", SqlDbType.VarChar,50),
        //            new SqlParameter("@DrugUseIdMeasu", SqlDbType.Decimal,9),
        //            new SqlParameter("@MeasuringUnitName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DrugFrequencyName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DrugUseOpenDose", SqlDbType.Int,4),
        //            new SqlParameter("@OpenDoseName", SqlDbType.VarChar,50),
        //            new SqlParameter("@DocAdviceId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DrugUsageName;
        //    parameters[2].Value = model.DrugUseIdMeasu;
        //    parameters[3].Value = model.MeasuringUnitName;
        //    parameters[4].Value = model.DrugFrequencyName;
        //    parameters[5].Value = model.DrugUseOpenDose;
        //    parameters[6].Value = model.OpenDoseName;
        //    parameters[7].Value = model.DocAdviceId;

        //    object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(ZX.HIS.Model.DrugUseInfo model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update tbDrugUseInfo set ");
        //    strSql.Append("HospitalId=@HospitalId,");
        //    strSql.Append("DrugUsageName=@DrugUsageName,");
        //    strSql.Append("DrugUseIdMeasu=@DrugUseIdMeasu,");
        //    strSql.Append("MeasuringUnitName=@MeasuringUnitName,");
        //    strSql.Append("DrugFrequencyName=@DrugFrequencyName,");
        //    strSql.Append("DrugUseOpenDose=@DrugUseOpenDose,");
        //    strSql.Append("OpenDoseName=@OpenDoseName,");
        //    strSql.Append("DocAdviceId=@DocAdviceId");
        //    strSql.Append(" where DrugUseId=@DrugUseId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DrugUsageName", SqlDbType.VarChar,50),
        //            new SqlParameter("@DrugUseIdMeasu", SqlDbType.Decimal,9),
        //            new SqlParameter("@MeasuringUnitName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DrugFrequencyName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DrugUseOpenDose", SqlDbType.Int,4),
        //            new SqlParameter("@OpenDoseName", SqlDbType.VarChar,50),
        //            new SqlParameter("@DocAdviceId", SqlDbType.Int,4),
        //            new SqlParameter("@DrugUseId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DrugUsageName;
        //    parameters[2].Value = model.DrugUseIdMeasu;
        //    parameters[3].Value = model.MeasuringUnitName;
        //    parameters[4].Value = model.DrugFrequencyName;
        //    parameters[5].Value = model.DrugUseOpenDose;
        //    parameters[6].Value = model.OpenDoseName;
        //    parameters[7].Value = model.DocAdviceId;
        //    parameters[8].Value = model.DrugUseId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int DrugUseId)
        //{

        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDrugUseInfo ");
        //    strSql.Append(" where DrugUseId=@DrugUseId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugUseId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugUseId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 批量删除数据
        ///// </summary>
        //public bool DeleteList(string DrugUseIdlist )
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDrugUseInfo ");
        //    strSql.Append(" where DrugUseId in ("+DrugUseIdlist + ")  ");
        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.DrugUseInfo GetModel(int DrugUseId)
        //{

        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select  top 1 DrugUseId,HospitalId,DrugUsageName,DrugUseIdMeasu,MeasuringUnitName,DrugFrequencyName,DrugUseOpenDose,OpenDoseName,DocAdviceId from tbDrugUseInfo ");
        //    strSql.Append(" where DrugUseId=@DrugUseId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugUseId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugUseId;

        //    ZX.HIS.Model.DrugUseInfo model=new ZX.HIS.Model.DrugUseInfo();
        //    DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
        //    if(ds.Tables[0].Rows.Count>0)
        //    {
        //        return DataRowToModel(ds.Tables[0].Rows[0]);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        //public ZX.HIS.Model.DrugUseInfo DataRowToModel(DataRow row)
        //{
        //    ZX.HIS.Model.DrugUseInfo model = new ZX.HIS.Model.DrugUseInfo();
        //    if (row != null)
        //    {
        //        if (row["DrugUseId"] != null && row["DrugUseId"].ToString() != "")
        //        {
        //            model.DrugUseId = int.Parse(row["DrugUseId"].ToString());
        //        }
        //        if (row["HospitalId"] != null && row["HospitalId"].ToString() != "")
        //        {
        //            model.HospitalId = int.Parse(row["HospitalId"].ToString());
        //        }
        //        if (row["DrugUsageName"] != null)
        //        {
        //            model.DrugUsageName = row["DrugUsageName"].ToString();
        //        }
        //        if (row["DrugUseIdMeasu"] != null && row["DrugUseIdMeasu"].ToString() != "")
        //        {
        //            model.DrugUseIdMeasu = decimal.Parse(row["DrugUseIdMeasu"].ToString());
        //        }
        //        if (row["MeasuringUnitName"] != null)
        //        {
        //            model.MeasuringUnitName = row["MeasuringUnitName"].ToString();
        //        }
        //        if (row["DrugFrequencyName"] != null)
        //        {
        //            model.DrugFrequencyName = row["DrugFrequencyName"].ToString();
        //        }
        //        if (row["DrugUseOpenDose"] != null && row["DrugUseOpenDose"].ToString() != "")
        //        {
        //            model.DrugUseOpenDose = int.Parse(row["DrugUseOpenDose"].ToString());
        //        }
        //        if (row["OpenDoseName"] != null)
        //        {
        //            model.OpenDoseName = row["OpenDoseName"].ToString();
        //        }
        //        if (row["DocAdviceId"] != null && row["DocAdviceId"].ToString() != "")
        //        {
        //            model.DocAdviceId = int.Parse(row["DocAdviceId"].ToString());
        //        }
        //    }
        //    return model;
        //}

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select DrugUseId,HospitalId,DrugUsageName,DrugUseIdMeasu,MeasuringUnitName,DrugFrequencyName,DrugUseOpenDose,OpenDoseName,DocAdviceId ");
        //    strSql.Append(" FROM tbDrugUseInfo ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top,string strWhere,string filedOrder)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select ");
        //    if(Top>0)
        //    {
        //        strSql.Append(" top "+Top.ToString());
        //    }
        //    strSql.Append(" DrugUseId,HospitalId,DrugUsageName,DrugUseIdMeasu,MeasuringUnitName,DrugFrequencyName,DrugUseOpenDose,OpenDoseName,DocAdviceId ");
        //    strSql.Append(" FROM tbDrugUseInfo ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获取记录总数
        ///// </summary>
        //public int GetRecordCount(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) FROM tbDrugUseInfo ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    object obj = DbHelperSQL.GetSingle(strSql.ToString());
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby );
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.DrugUseId desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tbDrugUseInfo T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///*
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@tblName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@fldName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@PageSize", SqlDbType.Int),
        //            new SqlParameter("@PageIndex", SqlDbType.Int),
        //            new SqlParameter("@IsReCount", SqlDbType.Bit),
        //            new SqlParameter("@OrderType", SqlDbType.Bit),
        //            new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
        //            };
        //    parameters[0].Value = "tbDrugUseInfo";
        //    parameters[1].Value = "DrugUseId";
        //    parameters[2].Value = PageSize;
        //    parameters[3].Value = PageIndex;
        //    parameters[4].Value = 0;
        //    parameters[5].Value = 0;
        //    parameters[6].Value = strWhere;	
        //    return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        //}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

